package persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.apache.log4j.Logger;
import persistence.dao.UsersDao;
import persistence.entity.UsersEntity;

public class UsersDaoImpl implements UsersDao {
	
	private static final Logger logger = Logger.getLogger(UsersDaoImpl.class);
	private static final String USERDAOIMPL_ERROR = "Could not logout: "; 
	private static final String USERDAOIMPL_SET = "Invalid Update User";
	
	private static final String UPDATE_USER = "UPDATE `users` set pass_user = ?";

	private static final String GET_LOGIN_QUERY = "select id_user, name_user, pass_user from users where name_user = ? and pass_user = ?";

	private Connection connection;
	public UsersDaoImpl(Connection conn){
		this.connection = conn;
	}

	@Override
	public Class<UsersEntity> getEntityClass() {
		return null;
	}

	@Override
	public List<UsersEntity> findAll() {
		return null;
	}

	@Override
	public List<UsersEntity> findByExample(UsersEntity exampleInstance) {
		return null;
	}

	@Override
	public int countAll() {
		return 0;
	}

	@Override
	public int countByExample(UsersEntity exampleInstance) {
		return 0;
	}

	@Override
	public UsersEntity insert(UsersEntity entity) {
		return null;
	}

	@Override
	public UsersEntity update(UsersEntity entity) {
		return null;
	}

	public UsersEntity getUsers(String name_user, String pass_user) {
		
		UsersEntity login = new UsersEntity();

		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (login != null) {
				stmt = connection.prepareStatement(UsersDaoImpl.GET_LOGIN_QUERY);
				stmt.setString(1, name_user);
				stmt.setString(2, pass_user);
			}
			rs = stmt.executeQuery();
			while (rs.next()) {
				login.setId_user(rs.getInt("id_user"));
				login.setName_user(rs.getString("name_user"));
				login.setPass_user(rs.getString("pass_user"));
			}
		} catch (Exception e) {
			logger.error(UsersDaoImpl.USERDAOIMPL_ERROR + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return login;
	}

	@Override
	public UsersEntity setUsers(String pass_user) {
		
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (pass_user != null)
			{
				stmt = connection.prepareStatement(UsersDaoImpl.UPDATE_USER);
				stmt.setString(1, pass_user);
				stmt.executeUpdate();
			}else{
				logger.error(UsersDaoImpl.USERDAOIMPL_SET);
			}
		} catch (Exception e) {
			logger.error(UsersDaoImpl.USERDAOIMPL_ERROR + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}
		
	@Override
	public List<UsersEntity> getUsers() {
		return null;
	}
	
	@Override
	public UsersEntity delete(int[] id_city) {
		return null;
	}
}
